<?php


namespace app\admin\controller\shop;

use app\common\controller\Adminbase;
use think\Db;

class Shopgh extends Adminbase
{
//    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $shop_id = $this->request->param('shop_id/d', 0);
        if ($this->request->isAjax()) {
            $where[] = ['is_del', '=', 0];
            $where[] = ['shop_id', '=', $shop_id];
            list($page, $limit) = $this->buildTableParames();
            $_list = Db::name('shop_gh')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total = Db::name('shop_gh')->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('shop_gh')
            ->where('id', $id)
            ->update(['status' => $status])) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
//                Db::name('shop_gh')->where('id', $id)->update([
//                    'is_del'=>1
//                ]);
                Db::name('shop_gh')->where('id', $id)->delete();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        $shop_id = $this->request->param('shop_id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['shop_id'] = $data['shop_id'];
            $data['row']['create_time'] = time();
            $stmt = Db::name('shop_gh')->insertGetId($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }

        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $this->assign([
            'proviec' => $proviec_list,
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $stmt = Db::name('shop_gh')
                ->where('id', '=', $data['id'])
                ->update($data['row']);
            $this->success('操作成功！');
            return $data;
        }
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();

        $data = Db::name('shop_gh')->find($id);
        $this->assign([
            'data' => $data,
            'proviec' => $proviec_list,
        ]);
        return $this->fetch();
    }

}